﻿using System;

// XNA lib
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;

//Oak Engine Lib
using OakEngine.GameState;
using OakEngine.Input;
using Microsoft.Xna.Framework.Input;

namespace MainJumpFleet.GameStates
{
   public partial class LevelDesignGameScreenState : OAK_IGameState
    {
        #region OAK_IGameState Members
        SpriteBatch batch;
        SpriteFont mainMenuFont;

        // holds the SpriteManager object
        private JumpFleetGameLib.xSprite.SpriteManager spriteManager;
              
        // holds the background music        
        private Song backgroundMusicSong;

        // flag for whether the background music is paused
        private bool isBackgroundMusicPaused;          

        // holds the textures
        private Texture2D starfieldBackgroundTexture;
        private Texture2D playerShipTexture;
        private Texture2D[] asteroidTextures;
        private Texture2D[] planetTextures;
        private Texture2D enemyShipFighterTexture;
        private Texture2D enemyShipCruiserTexture;

        // holds the sector map 
        private JumpFleetGameLib.xMap.SectorMap.SectorMap alpha1SectorMap;

        // holds the PlayerShip object
        private PlayerShip.PlayerShip playerShip;
        

        public LevelDesignGameScreenState()
        {
            Enter();
        }


        public void Enter()
        {
            mainMenuFont = CGame.CGame.GetInstance().Content.Load<SpriteFont>("Fonts\\mainMenuFont");
            batch = CGame.CGame.GetInstance().SpriteBatch;

            // creates the SpriteManager object
            spriteManager = new JumpFleetGameLib.xSprite.SpriteManager();

            LoadAssets();           

  
        }

        public bool Input(OAK_InputManager input)
        {
            

            if (input.Pressed(Keys.Escape) || input.Pressed(Buttons.Back, 0))
                CGame.CGame.GetInstance().ChangeState(new MainMenuState());

            // pauses the background music
            if ( input.Pressed(Keys.M) )
            {
                if (!isBackgroundMusicPaused)
                {
                    MediaPlayer.Pause();
                    isBackgroundMusicPaused = true;
                }               
            }

            // resumes the background music
            if ( input.Pressed(Keys.N) )
            {
                if (isBackgroundMusicPaused)
                {
                    MediaPlayer.Resume();
                    isBackgroundMusicPaused = false;
                }
            }    

            return false;
        }

        public void Update(GameTime dt)
        {
            // sends the GamePadState to the player ship object
            playerShip.Update(dt, CGame.CGame.GetInstance().Input.GamePads[0]);

            // updates the map objects
            alpha1SectorMap.UpdateObjects(dt);
        }

        public void Render()
        {
            // batch.DrawString(mainMenuFont, "Welcome to Main Game Screen State\n Press Esc to go back to Main menu ", new Vector2(300, 250), Color.White);

            // ends the SpriteBatch from CGame
            batch.End();

            // starts the local SpriteBatch
            batch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

            // let the SpriteManager do it's job
            spriteManager.Draw(batch);

            // ends the local SpriteBatch
            batch.End();

            // starts the SpriteBatch again so I don't break the other GameStates
            batch.Begin();

        }

        public void Exit()
        {
            MediaPlayer.Stop();
        }

        #endregion
    }
}
